package com.ecoolex.chinadutyfree.retailer.core.manager;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderAfterSalesDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSales;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSales.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSalesExample;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderAfterSalesExample.Criteria;
import com.ecoolex.chinadutyfree.retailer.enums.DirectMailGoodsOrderAfterSalesRecordType;
import com.ecoolex.chinadutyfree.retailer.enums.DirectMailGoodsOrderAfterSalesStatus;
import com.ecoolex.chinadutyfree.retailer.enums.DirectMailGoodsOrderAfterSalesType;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryDirectMailGoodsOrderAfterSalesDTO;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.exception.BizException;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.RandomUtil;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectMailGoodsOrderAfterSalesManager {
	
    @Autowired
    private DirectMailGoodsOrderAfterSalesDAO dao;
    
    @Autowired
    private DirectMailGoodsOrderAfterSalesRecordManager afterSalesRecordManager;
    
    public DirectMailGoodsOrderAfterSales create(Integer orderId,Integer userId,String reason,
		    	String evidenceImage,DirectMailGoodsOrderAfterSalesType type,
		    	DirectMailGoodsOrderAfterSalesStatus status,BigDecimal refundPrice,String createUser) {
    	DirectMailGoodsOrderAfterSales afterSales = new DirectMailGoodsOrderAfterSales();
    	afterSales.setAfterSalesNo(afterSalesNo());
    	afterSales.setOrderId(orderId);
    	afterSales.setUserId(userId);
    	afterSales.setType(type.real());
    	afterSales.setReason(reason);
    	afterSales.setEvidenceImage(evidenceImage);
    	afterSales.setRefundPrice(refundPrice);
    	afterSales.setStatus(status.real());
    	afterSales.setCreateUser(createUser);
    	afterSales.setCreateTime(Date.from(Instant.now()));
    	afterSales.setUpdateTime(Date.from(Instant.now()));
    	afterSales.setUpdateUser(createUser);
    	afterSales.setDeleted(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	dao.insert(afterSales);
    	return afterSales;
    }
    
    public DirectMailGoodsOrderAfterSales getAfterSalesPossibleBeDeletedByAfterSalesId(Integer afterSalesId) {
		return dao.selectByPrimaryKey(afterSalesId);
	}

	public Pagination<DirectMailGoodsOrderAfterSales> queryDirectMailGoodsOrderAfterSales(QueryDirectMailGoodsOrderAfterSalesDTO query, Sorting[] sorts, Page page) {
		return Pagination.create(page, 
			dao.count(query),
			() -> dao.query(query, Sortings.builder(sorts).toSql(), page));
	}
    
    private String afterSalesNo() {
    	String afterSalesNo = RandomUtil.randomInt(15);
    	DirectMailGoodsOrderAfterSales old = getByAfterSalesNo(afterSalesNo);
		if(Check.notNull(old)) {
			return afterSalesNo();
		}
		return afterSalesNo;
    }
    
    public DirectMailGoodsOrderAfterSales getByAfterSalesNo(String afterSalesNo){
    	DirectMailGoodsOrderAfterSalesExample example = new DirectMailGoodsOrderAfterSalesExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andAfterSalesNoEqualTo(afterSalesNo).andDeletedEqualTo(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	return dao.selectOneByExample(example);
    }
    
    public int update(DirectMailGoodsOrderAfterSales record,String updateUser) {
    	record.setUpdateTime(Date.from(Instant.now()));
    	record.setUpdateUser(updateUser);
    	return dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    }
    
    public DirectMailGoodsOrderAfterSales getByOrderId(Integer orderId) {
    	DirectMailGoodsOrderAfterSalesExample example = new DirectMailGoodsOrderAfterSalesExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	example.orderBy(Column.createTime.desc());
    	example.limit(1);
    	return dao.selectOneByExample(example);
    }
    
    public DirectMailGoodsOrderAfterSales getDirectMailGoodsOrderAfterSalesByAfterSalesId(Integer afterSalesId) {
    	DirectMailGoodsOrderAfterSalesExample example = new DirectMailGoodsOrderAfterSalesExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andAfterSalesIdEqualTo(afterSalesId).andDeletedEqualTo(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	return dao.selectOneByExample(example);
	}
    
    public DirectMailGoodsOrderAfterSales getDirectMailGoodsOrderAfterSalesByAfterSalesIdAndUserId(Integer afterSalesId, Integer userId) {
    	DirectMailGoodsOrderAfterSalesExample example = new DirectMailGoodsOrderAfterSalesExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andAfterSalesIdEqualTo(afterSalesId)
    	.andUserIdEqualTo(userId)
    	.andDeletedEqualTo(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	return dao.selectOneByExample(example);
	}
    
    public Pagination<DirectMailGoodsOrderAfterSales> queryDirectMailGoodsOrderAfterSalesForUser(Integer userId, Page page) {
    	DirectMailGoodsOrderAfterSalesExample example = new DirectMailGoodsOrderAfterSalesExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andUserIdEqualTo(userId).andDeletedEqualTo(DirectMailGoodsOrderAfterSales.NOT_DELETED);
    	example.setOffset(page.getFirstIndex());
    	example.setRows(page.getPageSize());
    	example.orderBy(Column.createTime.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

    /**
     * 用户寄回商品，填写物流单号
     * @param afterSales
     * @param deliveryNo
     * @param updateUser
     * @return
     * @throws BizException
     */
    public DirectMailGoodsOrderAfterSales writeDeliveryNo(DirectMailGoodsOrderAfterSales afterSales, String deliveryNo, String updateUser) throws BizException {
    	BizAssert.isTrue(DirectMailGoodsOrderAfterSalesStatus.canWriteDeliveryNo(afterSales.getStatus()),
            ResultCode.VERIFY.build(20),
            "不能填写物流单号");
    	afterSales.setStatus(DirectMailGoodsOrderAfterSalesStatus.WAIT_STORE_CONFIRM.real());
    	afterSales.setUpdateUser(updateUser);
    	afterSales.setUpdateTime(Date.from(Instant.now()));
    	dao.updateByPrimaryKeySelective(afterSales, Column.status,Column.updateUser,Column.updateTime);
    	//创建售后记录
    	afterSalesRecordManager.create(afterSales.getOrderId(), afterSales.getAfterSalesId(), DirectMailGoodsOrderAfterSalesRecordType.RETURN_GOODS, deliveryNo,null, updateUser);
    	return afterSales;
	}
}